package rdd.operate;

import org.apache.spark.SparkConf;
import org.apache.spark.api.java.JavaPairRDD;
import org.apache.spark.api.java.JavaRDD;
import org.apache.spark.api.java.JavaSparkContext;
import org.apache.spark.api.java.function.Function;

import java.util.Arrays;
import java.util.List;

public class Spark44_Operate_groupBy_1 {
    public static void main(String[] args) throws InterruptedException {
        final SparkConf conf = new SparkConf();
        conf.setMaster("local");
        conf.setAppName("spark");
        final JavaSparkContext jsc = new JavaSparkContext(conf);
        final List<Integer> nums = Arrays.asList(1,2,3,4);
        final JavaRDD<Integer> rdd = jsc.parallelize(nums , 2);
        JavaPairRDD<Object, Iterable<Integer>> rdd1 = rdd.groupBy(
                new Function<Integer, Object>() {
                    @Override
                    public Object call(Integer num) throws Exception {
                        return num % 2 == 0 ;                                   //因为num % 2 == 0 返回的就是true或者false，可以直接拿来当做组名
                    }
                }
                , 2);//groupBy具有改变分区的能力，第二个参数就可以实现
        rdd1.collect().forEach(System.out::println);
        jsc.close();
    }
}
